/*
 * Copyright (C) 2009, 2010, B3log Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.b3log.nide.action;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.b3log.latke.action.AbstractAction;
import org.b3log.latke.action.ActionException;
import org.b3log.latke.service.ServiceException;
import org.b3log.nide.Nide;
import org.b3log.nide.service.ProjectManager;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Project space tree action.
 *
 * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
 * @version 1.0.0.5, Mar 10, 2011
 */
public final class ProjectSpaceTreeAction extends AbstractAction {

    /**
     * Default serial version uid.
     */
    private static final long serialVersionUID = 1L;
    /**
     * Logger.
     */
    private static final Logger LOGGER =
            Logger.getLogger(ProjectSpaceTreeAction.class.getName());

    /**
     * Processes {@literal HTTP GET} method, gets a json object that depictes
     * a project space tree for the specified http servlet request and the
     * specified http servlet response.
     * <p>
     * The data structure of the project space tree refers to
     * <a href="http://code.google.com/p/jstree">jsTree.v.1.0rc</a>.
     * </p>
     *
     * @param request the specified http servlet request
     * @param response the specified http servlet response
     * @throws IOException io exception
     */
    @Override
    public void doGet(final HttpServletRequest request,
                      final HttpServletResponse response) throws IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        LOGGER.fine("Getting project space tree....");

        JSONObject projectSpaceTree = null;
        final ProjectManager projectManager = ProjectManager.INSTANCE;

        try {
            projectSpaceTree = projectManager.genProjectSpaceTree(
                    ProjectManager.getProjectsDir());
        } catch (final ServiceException e) {
            LOGGER.log(Level.SEVERE, e.getMessage(), e);
            throw new IOException(e);
        }

        final String ret = new String(projectSpaceTree.toString().getBytes(),
                                      "UTF-8");

        try {
            LOGGER.log(Level.FINER, "Project space tree: {0}",
                       projectSpaceTree.toString(Nide.INDENT));
        } catch (final JSONException e) {
            LOGGER.log(Level.SEVERE, e.getMessage(), e);
            throw new IOException(e);
        }

        final PrintWriter writer = response.getWriter();
        writer.write(ret);
        writer.close();
    }

    @Override
    protected Map<?, ?> doFreeMarkerAction(
            final freemarker.template.Template template,
            final HttpServletRequest request,
            final HttpServletResponse response) throws ActionException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected JSONObject doAjaxAction(final JSONObject data,
                                      final HttpServletRequest request,
                                      final HttpServletResponse response)
            throws ActionException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
